package com.drondea.testclient.handler.sgip.server;

import cn.hutool.core.util.StrUtil;
import com.drondea.sms.channel.ChannelSession;
import com.drondea.sms.common.util.CommonUtil;
import com.drondea.sms.message.sgip12.SgipReportRequestMessage;
import com.drondea.sms.message.sgip12.SgipReportResponseMessage;
import com.drondea.testclient.panel.SgipPanel;
import com.drondea.testclient.util.counter.SgipAtomicUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * 测试服务器接收短信数据
 * @author shd
 */

@Slf4j
public class ServerSgipReportRequestHandler extends SimpleChannelInboundHandler<SgipReportRequestMessage> {

    private static final SgipPanel PANEL = SgipPanel.getInstance();

    /**
     * Report命令用于向SP发送一条先前的Submit命令的当前状态
     * 
     * @param ctx ChannelHandlerContext
     * @param msg Sgip回执消息
     */
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, SgipReportRequestMessage msg) {
        log.debug("SgipReportRequestMessage msgId: {}, status: {}, errCode: {}", msg.getSubmitSequenceNumber(), msg.getState(), msg.getErrorCode());
        PANEL.reportCountLabel.setText(StrUtil.format("报告数量: {}", SgipAtomicUtil.REPORT_COUNT.incrementAndGet()));

        ChannelSession channelSession = CommonUtil.getChannelSession(ctx.channel());
        SgipReportResponseMessage response = new SgipReportResponseMessage(msg.getHeader());
        response.setResult((short) 0);
        channelSession.sendMessage(response);
    }
}
